package net.oschina.git.hikoyan.helloapi.controller;


import net.oschina.git.hikoyan.helloapi.authentication.UserInfo;
import net.oschina.git.hikoyan.helloapi.shiro.jwt.repository.UserRepository;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.json.Json;
import javax.json.JsonObjectBuilder;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response;

/**
 * Created by yan on 17/1/17.
 */
@RestController
public class LoginController {

    @Autowired
    private UserRepository shiroBoundary;

    @RequestMapping("/login")
    public Response login(HttpServletResponse response) throws Exception {
        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        return Response.status(200).entity(shiroBoundary.createToken(userInfo)).build();
    }

    @RequestMapping("/secured")
    public Response secured() throws Exception {
        JsonObjectBuilder json = Json.createObjectBuilder();
        json.add("message", "It works!!");
        return Response.status(200).entity(json.build()).build();
    }
}
